const util = require("../../../utils/util");
const moment = require("../../../utils/moment");
// pages/my/feedbackList/feedbackList.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    token: "",
    listData: [], //  反馈内容
    currentPage: 1, //  当前页数
    totalPage: "", //  总页数
  },
  openNew() {
    wx.navigateTo({
      url: "../addFeedback/addFeedback",
    });
  },
  gotoDetail(e) {
    console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
      url: "../feedbackDetail/feedbackDetail?id=" + e.currentTarget.dataset.id,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var token = wx.getStorageSync("token");
    let that = this;
    this.setData({
      token,
    });
    this.getData(1);
    this.getQuestion();
  },
  getData(pageNum) {
    var that = this;
    var param = {
      feedback_type: 2,
      page: that.data.currentPage || pageNum,
      per_page: 5,
    };
    util.getToken("/wechat/feedbacks", param, that.data.token, function (res) {
      // console.log(res.data.data);
      let arr = res.data.data;
      arr.forEach(function (item, index) {
        item.updated_at = moment(item.updated_at).format("YYYY/MM/DD hh:mm:ss");
        item.question_type = that.concatStr(item.question_type.toString());
      });
      if (pageNum) {
        that.setData({
          listData: arr,
          totalPage: res.data.last_page,
          currentPage: res.data.current_page,
        });
      } else {
        that.setData({
          listData: that.data.listData.concat(arr),
          totalPage: res.data.last_page,
          currentPage: res.data.current_page,
        });
      }
    });
  },
  // 获取问题类型
  getQuestion() {
    let that = this;
    util.postRes("/wechat/feedback/question/types", {}, function (res) {
      // console.log(typeof res);
      let arr = [];
      for (let i in res) {
        arr.push({
          id: i,
          name: res[i],
        });
      }
      // console.log(arr);
      that.setData({
        questionData: arr,
      });
    });
  },
  concatStr(str) {
    // console.log("刚开始", str);
    // console.log(typeof str);
    let that = this;
    let questionData = that.data.questionData;
    // let str = "1,4,5,7";
    let arr = str.split(",");
    let arr1 = [];
    for (let i = 0; i < arr.length; i++) {
      let index = arr[i] - 1;
      arr1.push(questionData[index].name);
    }
    // console.log(arr1);
    // console.log(arr1.join(","));
    return arr1.join(",");
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getData(1);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("下拉");
    this.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    console.log(that.data.currentPage);
    console.log(that.data.totalPage);
    if (that.data.currentPage < that.data.totalPage) {
      that.setData({
        currentPage: that.data.currentPage + 1,
      });
      that.getData();
    } else {
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
